package com.library.controller;


import com.library.dao.db.JdbcAdminRepository;
import com.library.dao.db.JdbcUserRepository;
import com.library.entity.Admin;
import com.library.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

@Controller
public class ListController {
    @Autowired
    private JdbcAdminRepository jdbcAdminRepository;
    @Autowired
    private JdbcUserRepository jdbcUserRepository;


    /**
     *
     * @param page 当前页数
     * @param model model
     * @return adminList
     */
    @GetMapping("/adminList")
    public String adminList(@RequestParam(defaultValue = "1")int page, Model model) {
        int pageSize = 5;
        int offset=(page-1)*pageSize;

        List<Admin> adminList=jdbcAdminRepository.findByPage(offset, pageSize);
        int totalAdminCount=jdbcAdminRepository.countAdmins();
        int totalAdminPages =(int)Math.ceil((double) totalAdminCount/pageSize);

        model.addAttribute("currentAdminPage", page);
        model.addAttribute("totalAdminPages", totalAdminPages);
        model.addAttribute("adminList",adminList);
        return "adminList";
    }

    /**
     *
     * @param userName 用户名
     * @param password 密码
     * @param fullName 全名
     * @param email 邮件
     * @param phoneNo 电话号码
     * @param model model
     * @return 重定向adminList
     */
    @PostMapping("/addAdmin")
    public String addAdmin(@RequestParam("userName")String userName,
                           @RequestParam("password")String password,
                           @RequestParam("fullName")String fullName,
                           @RequestParam("email")String email,
                           @RequestParam("phoneNo")String phoneNo,
                           Model model) {
       jdbcAdminRepository.addAdmin(new Admin(0l,userName,password,fullName,email,phoneNo,false));

       return "redirect:/adminList";
    }

    /**
     *
     * @param userName 用户名
     * @param model 获取model
     * @return 重定向/adminList
     */
    @PostMapping("/deleteAdmin")
    public String deleteAdmin(@RequestParam("userName")String userName,
                              Model model){
        jdbcAdminRepository.deleteByName(userName);
        return "redirect:/adminList";
    }

    /**
     *
     * @param id 隐性传入id
     * @param new_password 新密码
     * @param model 获取model
     * @return 重定向 adminList
     */
    @PostMapping("/changeAdminPassword")
    public String changePassword(@RequestParam("id")Long id,
                                 @RequestParam("new_password")String new_password,
                                 Model model){
        jdbcAdminRepository.updateAdmin(new_password,id);
        return "redirect:/adminList";
    }

    /**
     *
     * @param page 当前页数
     * @param model 获取model
     * @return userList
     */
    @GetMapping("/userList")
    public String userList(@RequestParam(defaultValue = "1")int page,Model model){
        int pageSize = 5;
        int offset=(page-1)*pageSize;

        List<User> userList=jdbcUserRepository.findByPage(offset, pageSize);
        int totalUserCount=jdbcUserRepository.countUsers();
        int totalUserPages=(int)Math.ceil((double) totalUserCount/pageSize);

        model.addAttribute("currentUserPage", page);
        model.addAttribute("totalUserPages", totalUserPages);
        model.addAttribute("userList",userList);
        return "userList";
    }

    /**
     *
     * @param userName 用户名
     * @param password 密码
     * @param fullName 全名
     * @param email 邮件
     * @param phoneNo 电话
     * @param model 获取model
     * @return 重定向userList
     */
    @PostMapping("/addUser")
    public String addUser(@RequestParam("userName")String userName,
                          @RequestParam("password")String password,
                          @RequestParam("fullName")String fullName,
                          @RequestParam("email")String email,
                          @RequestParam("phoneNo")String phoneNo,
                          Model model){
        jdbcUserRepository.addUser(new User(0L,userName,password,fullName,email,phoneNo,false,new Date()));

        return "redirect:/userList";
    }

    /**
     *
     * @param userName 用户名
     * @param model 获取model
     * @return 重定向userList
     */
    @PostMapping("/deleteUser")
    public String deleteUser(@RequestParam("userName")String userName,
                             Model model){
        jdbcUserRepository.deleteByName(userName);
        return "redirect:/userList";
    }

    /**
     *
     * @param id 隐性传入id
     * @param new_password 新密码
     * @param model 获取model
     * @return 重定向userList
     */
    @PostMapping("/changeUserPassword")
    public String changeUserPassword(@RequestParam("id")Long id,
                                     @RequestParam("new_password")String new_password,
                                     Model model){
        jdbcUserRepository.updateUser(new_password,id);
        return "redirect:/userList";
    }


}
